<?php
declare(strict_types=1);

// +----------------------------------------------------------------------
// | 兔兔答题考试系统
// +----------------------------------------------------------------------
// | 感谢使用兔兔答题考试系统
// | 本系统经过商业授权，不能转售、开源或者以其他方式进行使用，只可在购买协议范围内容使用
// | 若发现未被版权协议，将追究相应的公司、团队、企业等
// | 访问官网：https://www.tutudati.com
// | 兔兔答题考试系统开发者版权所有 拥有最终解释权
namespace App\Logic\User\Service\Audio;

use App\Logic\User\Repository\Audio\AudioResourceRepository;
use App\Logic\User\Service\BaseService;
use App\Logic\User\Service\UserServiceInterface;
use Closure;
use Illuminate\Support\Facades\DB;

class AudioResourceService extends BaseService implements UserServiceInterface
{
    public static function searchWhere(array $requestParams): Closure
    {
        return function ($query) use ($requestParams) {
            if (!empty($requestParams["uid"])) {
                $query->where("uid", "=", $requestParams["uid"]);
            }
            if (!empty($requestParams["tag_uid"])) {
                $query->where("tag_uid", "=", $requestParams["tag_uid"]);
            }
            if (!empty($requestParams["title"])) {
                $query->where("title", "like", "%" . $requestParams['title'] . "%");
            }
            $query->where("is_show", "=", 1);
        };
    }

    public function serviceSelect(array $requestParams): array
    {
        return (new AudioResourceRepository)->repositorySelect(self::searchWhere($requestParams),
            (int)($requestParams["size"] ?? 20),
            ["uid", "title", "time", "remark", "collection", "download", "tag_uid", "url", "path", "image"]);
    }

    public function serviceRelSelect(array $requestParams): array
    {
        return (new AudioResourceRepository)->repositorySelect(self::searchWhere($requestParams),
            (int)($requestParams["size"] ?? 10000),
            ["uid", "title"]);
    }

    public function serviceCollection(string $uid): bool
    {
        return (bool)(new AudioResourceRepository())->repositoryUpdate([["uid", "=", $uid]],
            ["collection" => DB::raw("collection+1")]);
    }

    public function serviceShare(string $uid): bool
    {
        return true;
    }


    public function serviceCreate(array $requestParams): array
    {
        return [];
    }

    public function serviceUpdate(array $requestParams): array
    {
        return [];
    }

    public function serviceDelete(array $requestParams): int
    {
        return 0;
    }

    public function serviceFind(array $requestParams): array
    {
        return (new AudioResourceRepository())->repositoryFind(self::searchWhere($requestParams),
            ["uid", "title", "time", "remark", "collection", "download", "tag_uid", "url", "path", "image"]);
    }
}
